/**
 * Created by lenovo on 2017/7/20.
 */
let DBPool = require('../dao/DBConnectPool');
let async = require('async');
let config = require('../../config');
let WXMember = require('./WXMemberController');
const request = require('request');

module.exports = {
    /**
     * 点播影院首页
     * 获取广告位
     */
    showRecommendedCinemas: function (req, res) {
        let title = '推荐影院';
        WXMember.getMemberInformation(req,res,function (err, code, result) {
            if(code){
                res.render('wx-404', {
                    title: title,
                    errorMessage: err
                });
            } else {
                DBPool.getPoolConnection('mtsc', function (err, connection) {
                    if (err) {
                        res.render('wx-404', {
                            title: title,
                            errorMessage: '数据库链接错误'
                        });
                        connection.release();
                        return;
                    }
                    let sql = 'SELECT * FROM cinema_banner WHERE banner_status = 1 ORDER BY banner_order ASC ';
                    connection.query(sql, null, function (queryErr, rows) {
                        if (queryErr) {
                            res.render('wx-404', {
                                title: title,
                                errorMessage: '获取初始化信息（广告内容）发生错误!'
                            });
                            connection.release();
                        } else {
                            WXMember.queryConfig(req,function (data) {
                                res.render('wx-recommendedcinemas', {
                                    title: title,
                                    sdkKey: config.amap.sdkKey,
                                    open_id: result.openid,
                                    appId: config.app.appid,
                                    sign_pkg: data.jsapi_ticket,
                                    timestamp: data.timestamp,
                                    nonceStr: data.nonceStr,
                                    signature: data.signature,
                                    ak: config.bmap.AK,

                                    banners: rows
                                });
                                connection.release();
                            });

                        }
                    });
                });
            }
        });
    },

    /**
     * 获影院列表
     * @param req
     * @param res
     */
    getRecommendedCinemas: function (req, res) {
        DBPool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                res.json({errorMessage: '数据库链接错误'});
                connection.release();
                return;
            }

            let limit = 10;
            let page = req.body.page;
            let lat = req.body.lat;
            let lng = req.body.lng;

            console.log('getRecommendedCinemas'+lng);
            console.log('getRecommendedCinemas'+lat);

            let key = req.body.searchKey;
            let region = req.body.region_search;
            let sort = req.body.intelligent_sort;
            let type = req.body.filtrate_type;

            let WHERE = " WHERE a.biz_status = 2 ";
            //let WHERE = " WHERE 1 = 1 ";
            let JOIN = " INNER JOIN cinema_region AS b ON a.cinema_regionid = b.id ";
            let whereArray = [];

            //影吧名称暧昧检索
            if (key) {
                WHERE += " AND (a.cinema_name LIKE ? OR a.cinema_site LIKE ?) ";
                whereArray.push("%" + key + "%");
                whereArray.push("%" + key + "%");
            }
            //地域范围检索
            let distance25KM = 0.25;
            let distance5KM = 0.05;
            let distance2KM = 0.02;
            let distance1KM = 0.01;
            let distance500M = 0.005;

            let x1, x2, y1, y2;
            if (region) {
                if (region == "附近") {
                    x1 = lat - distance25KM;
                    x2 = lat + distance25KM;
                    y1 = lng - distance25KM;
                    y2 = lng + distance25KM;
                    WHERE += " AND a.cinema_lat>" + x1 + " AND a.cinema_lat <" + x2 + " AND a.`cinema_long`>" + y1 + " AND a.`cinema_long`<" + y2;
                } else if (region == "500米") {
                    x1 = lat - distance500M;
                    x2 = lat + distance500M;
                    y1 = lng - distance500M;
                    y2 = lng + distance500M;
                    WHERE += " AND a.cinema_lat>" + x1 + " AND a.cinema_lat <" + x2 + " AND a.`cinema_long`>" + y1 + " AND a.`cinema_long`<" + y2;
                } else if (region == "1000米") {
                    x1 = lat - distance1KM;
                    x2 = lat + distance1KM;
                    y1 = lng - distance1KM;
                    y2 = lng + distance1KM;
                    WHERE += " AND a.cinema_lat>" + x1 + " AND a.cinema_lat <" + x2 + " AND a.`cinema_long`>" + y1 + " AND a.`cinema_long`<" + y2;

                } else if (region == "2000米") {
                    x1 = lat - distance2KM;
                    x2 = lat + distance2KM;
                    y1 = lng - distance2KM;
                    y2 = lng + distance2KM;
                    WHERE += " AND a.cinema_lat>" + x1 + " AND a.cinema_lat <" + x2 + " AND a.`cinema_long`>" + y1 + " AND a.`cinema_long`<" + y2;
                } else if (region == "5000米") {
                    x1 = lat - distance5KM;
                    x2 = lat + distance5KM;
                    y1 = lng - distance5KM;
                    y2 = lng + distance5KM;
                    WHERE += " AND a.cinema_lat>" + x1 + " AND a.cinema_lat <" + x2 + " AND a.`cinema_long`>" + y1 + " AND a.`cinema_long`<" + y2;
                } else {
                    WHERE += " AND a.region_str LIKE ? ";
                    whereArray.push("%" + region + "%");
                }
            }

            //筛选条件检索
            if (type) {
                if (type == 0) {           //全部
                } else if (type == 1) {    //可预约
                    WHERE += " AND (a.api_key is NOT NULL AND api_key <> '') ";
                } else if (type == 2) {    //不可预约
                    WHERE += " AND (a.api_key is NULL OR api_key = '') ";
                }
            }

            //排序
            let ORDERBY = " ORDER BY distance ";
            if (sort) {
                if (sort == 0) {           //按距离
                } else if (sort == 1) {    //按价格由低到高
                    ORDERBY = " ORDER BY price ASC ";
                } else if (sort == 2) {    //按价格由高到低
                    ORDERBY = " ORDER BY price DESC ";
                }
            }

            let sql = "SELECT a.cinema_code, a.cinema_name, a.score,a.cinema_picture, a.cinema_long, a.cinema_lat,a.tag_1,a.tag_2,a.tag_3,a.tag_4,a.tag_5,a.tag_6,a.tag_7,a.tag_8, ";
            sql += " (a.cinema_lat - " + lat + ") * (a.cinema_lat - " + lat + ") + (a.`cinema_long` - " + lng + ") * (a.`cinema_long` - " + lng + ") AS distance, ";
            sql += " a.cinema_site, a.price ,b.name AS region_name FROM cinema AS a ";
            sql += JOIN;
            sql += WHERE;
            sql += ORDERBY;
            sql += " LIMIT ?,?";

            whereArray.push(page * limit);
            whereArray.push(limit);
            connection.query(sql, whereArray, function (queryErr, rows) {
                console.log(sql);
                console.log(whereArray);
                if (queryErr) {
                    res.json({errorMessage: '获取影院影片列表失败'});
                } else {
                    res.json({
                        isLast: rows.length < limit ? 1 : 0,
                        cinemas: rows
                    });
                }
                connection.release();
            });

        });
    },

    /**
     * 搜索影院
     * @param req
     * @param res
     */
    getSearchedCinemas:function (req, res) {
        let searchKey =  req.body.searchKey;
        searchKey= searchKey.replace(/([\-\_\,\!\|\~\`\(\)\#\$\%\^\&\*\{\}\:\;\"\L\<\>\?])/g, '\\$1');
        searchKey = '%' + searchKey + '%';

        let lat = req.body.lat;
        let lng = req.body.lng;
        console.log('getSearchedCinemas',searchKey);
        console.log(lat);
        console.log(lng);

        DBPool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                res.json({errorMessage: '数据库链接错误'});
                connection.release();
                return;
            }
            let sql = "SELECT a.cinema_code, a.cinema_name, a.score, a.cinema_long, a.cinema_lat,a.tag_1,a.tag_2,a.tag_3,a.tag_4,a.tag_5,a.tag_6,a.tag_7,a.tag_8, ";
            sql += " (a.cinema_lat - " + lat + ") * (a.cinema_lat - " + lat + ") + (a.`cinema_long` - " + lng + ") * (a.`cinema_long` - " + lng + ") AS distance, ";
            sql += " a.cinema_site, b.name AS region_name,a.cinema_picture,a.price ";
            sql += " FROM cinema AS a ";
            sql += " INNER JOIN cinema_region AS b ON a.cinema_regionid = b.id ";
            sql += " AND (a.cinema_name LIKE ? OR a.cinema_site LIKE ?) ";
            sql += " AND a.biz_status = 2 ";
            sql += " ORDER BY distance ";

            connection.query(sql, [searchKey,searchKey], function (queryErr, rows) {

                console.log(sql);
                console.log(searchKey);

                if (queryErr) {
                    res.json({errorMessage: '数据库链接错误'});
                    connection.release();
                } else {
                    res.json({
                        cinemas: rows,
                    });
                    connection.release();
                }
            });
        });
    },

    /**
     * 获取一级城市列表
     * @param req
     * @param res
     */
    showCityHome: function (req, res) {
        DBPool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                res.render('wx-cityhome', {errorMessage: '数据库链接错误'});
                connection.release();
                return;
            }
            let sql = 'SELECT id,name,hot_flag,citycode,UPPER(left(`enname`,1)) AS py ' +
                ' FROM cinema_region ' +
                ' WHERE pid = ? ' +
                ' AND vaild_flag = 1 ' +
                ' ORDER BY py ';
            connection.query(sql, [1], function (queryErr, rows) {
                if (queryErr) {
                    res.render('wx-cityhome', {errorMessage: '获取城市列表发生错误'});
                } else {
                    let cityList = [];
                    let hot = {
                        py: '热门',
                        list: []
                    };
                    for (let i = 0; i < rows.length; i++) {
                        if (rows[i].hot_flag == 1) {
                            hot.list.push(rows[i]);
                        }
                    }
                    cityList.push(hot);

                    for (let i = 0; i < rows.length; i++) {
                        if (i == 0 || rows[i].py != rows[i - 1].py) {
                            var tmp = {
                                py: '',
                                list: []
                            };
                            tmp.py = rows[i].py;
                            cityList.push(tmp);
                        }
                        tmp.list.push(rows[i]);
                    }


                    res.render('wx-cityhome', {citylist: cityList});
                }
                connection.release();
            });
        });
    },

    /**
     * 由定位获取的citycode获取city对象
     * @param req
     * @param res
     */
    getCityByCode:function (req,res) {
        let code = req.body.city_code;

        DBPool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                res.json({errorMessage: '数据库链接错误'});
                connection.release();
                return;
            }
            let sql = 'SELECT * ' +
                ' FROM cinema_region ' +
                ' WHERE citycode = ? ';
            connection.query(sql, [code], function (queryErr, rows) {
                if (queryErr) {
                    res.json({errorMessage: '获取城市信息失败'});
                } else {
                    if (rows.length == 0){
                        res.json({errorMessage: '获取城市信息失败'});
                    }else {
                        res.json(rows[0]);
                    }
                }
                connection.release();
            });
        });
    },

    /**
     * 由经纬度获取city对象
     * @param req
     * @param res
     */
    getCityByLatLng:function (req, res) {
        const aMapUrl =  'http://restapi.amap.com/v3/geocode/regeo';
        let lat = req.body.lat;
        let lng = req.body.lng;
        let url = aMapUrl + '?key=' + config.amap.AmapKey + '&location=' + lng + ',' + lat + '&radius=0&extensions=base&batch=false';

        request.get(url, function (err, response, body) {
            try {
                var responseJson = JSON.parse(body);
                // 解析成功
                if (responseJson['status'] == 1) {
                    // 如果返回正常解析数据
                    var citycode = responseJson['regeocode']['addressComponent']['citycode']; // 提取城市code
                    var adcode = responseJson['regeocode']['addressComponent']['adcode'];
                    console.log('WXCinemasController.js'+citycode);
                    console.log('WXCinemasController.js'+adcode);
                    console.log('WXCinemasController.js'+url);

                    DBPool.getPoolConnection('mtsc', function (err, connection) {
                        if (err) {
                            res.json({errorMessage: '数据库链接错误'});
                            connection.release();
                            return;
                        }
                        let sql = 'SELECT * ' +
                            ' FROM cinema_region ' +
                            ' WHERE citycode = ? ';
                        connection.query(sql, [citycode], function (queryErr, rows) {
                            if (queryErr) {
                                res.json({errorMessage: '获取城市信息失败'});
                            } else {
                                if (rows.length == 0){
                                    res.json({errorMessage: '获取城市信息失败'});
                                }else {
                                    res.json(rows[0]);
                                }
                            }
                            connection.release();
                        });
                    });
                } else {
                    res.json({errorMessage: '获取城市信息失败'});
                }
            } catch (error) {
                res.json({errorMessage: '获取城市信息失败'});
            }
        })
    },
    /**
     * 由城市获取商圈信息
     * @param req
     * @param res
     */
    getRegionOptions: function (req, res) {
        let cityId = req.body.cityid;
        let lat = req.body.lat;
        let lng = req.body.lng;
        //取得城市ID字符串
        let cityIdStr = "%|" + cityId + "|%";

        // console.log('getRegionOptions:' +cityId);
        // console.log('getRegionOptions:' +lat);
        // console.log('getRegionOptions:' +lng);
        // console.log('getRegionOptions:' +cityIdStr);
        //初始化
        let firstregionlist = [];
        DBPool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                res.json({errorMessage: '数据库链接错误'});
                connection.release();
                return;
            }
            async.waterfall(
                [
                    function (callback) {
                        let sql = 'SELECT name ' +
                            ' FROM cinema_region ' +
                            ' WHERE id = ? ';
                        connection.query(sql, [cityId], function (queryErr, rows) {
                            if (queryErr) {
                                callback(queryErr);
                            } else {
                                // console.log('debug1:', rows);
                                if (rows.length == 0) {
                                    callback('没有找到相关城市！');
                                } else {
                                    callback(null, rows[0].name);
                                }
                            }
                        });
                    },
                    function (arg1, callback) {
                        if (lat && lng) {
                            //搜索附近影吧
                            let distance = 0.25;    //25KM
                            let x1 = lat - distance;
                            let x2 = lat + distance;
                            let y1 = lng - distance;
                            let y2 = lng + distance;

                            let sql = 'SELECT cinema_id ' +
                                ' FROM cinema ' +
                                ' WHERE biz_status = 2 ' +
                                ' AND region_str LIKE ? ' +
                                ' AND cinema_lat > ? AND cinema_lat < ? AND `cinema_long` > ? AND `cinema_long` < ? ';

                            // console.log('getRegionOptions(381):', sql);
                            // console.log('getRegionOptions(382):', x1);
                            // console.log('getRegionOptions(383):', x2);
                            // console.log('getRegionOptions(384):', y1);
                            // console.log('getRegionOptions(385):', y2);

                            connection.query(sql, [cityIdStr, x1, x2, y1, y2], function (queryErr, rows) {
                                if (queryErr) {
                                    console.log('debug2:', queryErr);
                                    callback(queryErr);
                                } else {
                                    console.log('debug2:', rows)
                                    if (rows.length > 0) {
                                        let firstregion = {
                                            id: 0,
                                            name: "附近",
                                            secondregions: []
                                        };
                                        let secondregion = {
                                            id: 0,
                                            name: "附近"
                                        };
                                        firstregion.secondregions.push(secondregion);
                                        secondregion = {
                                            id: 1,
                                            name: "500米"
                                        };
                                        firstregion.secondregions.push(secondregion);
                                        secondregion = {
                                            id: 2,
                                            name: "1000米"
                                        };
                                        firstregion.secondregions.push(secondregion);
                                        secondregion = {
                                            id: 3,
                                            name: "2000米"
                                        };
                                        firstregion.secondregions.push(secondregion);
                                        secondregion = {
                                            id: 4,
                                            name: "5000米"
                                        };
                                        firstregion.secondregions.push(secondregion);
                                        firstregionlist.push(firstregion);
                                    }
                                    callback(null, arg1, firstregionlist);
                                }
                            });
                        } else {
                            // console.log('getRegionOptions(430):', firstregionlist);
                            callback(null, arg1, firstregionlist);
                        }

                    },
                    function (arg1, arg2, callback) {
                        let sql = 'SELECT id, name ' +
                            ' FROM cinema_region ' +
                            ' WHERE pid = ? ' +
                            // ' AND vaild_flag = 1 ' +
                            ' ORDER BY id ';
                        connection.query(sql, [cityId], function (queryErr, rows) {
                            if (queryErr) {
                                callback(queryErr);
                            } else {
                                callback(null,arg1, arg2,rows);
                            }
                        });
                    },
                    function (arg1, arg2, arg3, callback) {
                        let sql = 'SELECT id, name ' +
                            ' FROM cinema_region ' +
                            ' WHERE pid = ? ' +
                            // ' AND vaild_flag = 1 ' +
                            ' ORDER BY id ';
                        //城市全体
                        //城市全体
                        let firstregion = {
                            id: cityId,
                            name: arg1,
                            secondregions: [
                                {
                                    id: 0,
                                    name: "全部"
                                }
                            ]
                        };
                        firstregionlist.push(firstregion);
                        if (arg3.length == 0){
                            callback(null, arg1, arg2, arg3,firstregionlist);
                        }
                        for (let i = 0; i < arg3.length; i++) {
                            let tmp = {
                                id:arg3[i].id,
                                name:arg3[i].name,
                                secondregions:[{
                                    id: 0,
                                    name: "全部 " + arg3[i].name
                                }]
                            };
                            connection.query(sql, [arg3[i].id], function (queryErr, rows) {
                                if (queryErr) {
                                    callback(queryErr);
                                } else {
                                    if (rows.length == 0){
                                        callback(null, arg1, arg2, arg3,firstregionlist);
                                    }
                                    for (let l=0;l<rows.length;l++){
                                        tmp.secondregions.push({
                                            id: rows[l].id,
                                            name: rows[l].name
                                        });
                                        if (l === rows.length - 1) {
                                            //console.log('getRegionOptions(497):', tmp);
                                            firstregionlist.push(tmp);
                                            //console.log('getRegionOptions(500):', firstregionlist);
                                        }
                                    }
                                    if (i === arg3.length-1){
                                        // console.log('getRegionOptions(499):', firstregionlist);
                                        callback(null, arg1, arg2, arg3,firstregionlist);
                                    }
                                }
                            });
                        }
                    }

                ], function (err, arg1, arg2, arg3,arg4) {
                    if (err) {
                        console.log('getRegionOptions(504):', err);
                        res.json({
                            errorMessage: err
                        });
                        connection.release();
                        return;
                    }
                    // console.log('getRegionOptions(513):', arg4);
                    res.json(arg4);
                    connection.release();
                });
        });

    },

    /**
     * 影院详情页
     * @param req
     * @param res
     */
    showCinemaHome: function (req, res) {
        let cinemaCode = req.query.cinema_code;
        let from = req.query.from;
        let lat = req.query.lat;
        let lng = req.query.lng;
        let title = '影院详情';
        let openId = null;

        console.log('showCinemaHome'+lng);
        console.log('showCinemaHome'+lat);

        if (req.signedCookies.userinfo && req.signedCookies.userinfo.openid ){
            openId = req.signedCookies.userinfo.openid;
        }

        DBPool.getPoolConnection('mtsc', function (err, connection) {
            if (err) {
                res.render('wx-cinemahome', {
                    title: title,
                    errorMessage: '数据库链接错误'
                });
                console.log('1');
                connection.release();
                return;
            }
            async.waterfall([
                function (callback) {
                    let sql = 'SELECT c.*,r.name as region_name,' +
                        ' (c.cinema_lat - ?) * (c.cinema_lat - ?) + (c.`cinema_long` - ?) * (c.`cinema_long` - ?) AS distance ' +
                        ' FROM cinema as c ' +
                        ' INNER JOIN cinema_region as r ON r.id = c.cinema_regionid ' +
                        ' WHERE c.cinema_code = ? ';
                    connection.query(sql,[lat,lat,lng,lng,cinemaCode] ,function (queryErr, rows) {
                        if (queryErr) {
                            console.log('2');
                            callback('获取影院信息失败！');
                        } else {
                            if (rows.length == 0){
                                console.log('3');
                                callback('没有找到相关影院！');
                            } else {
                                console.log('4');
                                callback(null, rows[0])
                            }
                        }
                    })
                },
                function (arg1,callback) {

                    let sql = 'SELECT * ' +
                        ' FROM cinema_poster ' +
                        ' WHERE cinema_code = ? ' +
                        ' AND status = ? ';
                    connection.query(sql, [cinemaCode,1], function (queryErr, rows) {
                        if (queryErr) {
                            console.log('5');
                            callback('获取影院图集失败！');
                        } else {
                            console.log('6');
                            if (rows.length == 0) {
                                arg1.posters = null;
                            } else {
                                arg1.posters = rows;
                                // arg1.posters = [];
                                // for (let i=0;i<rows.length;i++){
                                //     let tmp = {
                                //         poster_id:rows[i].poster_id,
                                //         cinema_code:rows[i].cinema_code,
                                //         poster_url:rows[i].poster_url,
                                //         score:rows[i].score,
                                //     };
                                //     arg1.posters.push(tmp);
                                // }
                            }
                            callback(null,arg1);
                        }
                    });
                },
                function (arg1,callback) {
                    if (from){
                        arg1.movies = null;
                        callback(null,arg1);
                    } else {
                        let sql = 'SELECT DISTINCT m.mid,m.poster,m.movie_name,m.score ' +
                            ' FROM movie as m  ' +
                            ' INNER JOIN movie_package_detail as p ON p.mid = m.mid ' +
                            ' INNER JOIN contract as c ON c.contract_code = p.contract_code ' +
                            ' WHERE c.cinema_code = ? ' +
                            ' AND c.`status` = 1 ' +
                            ' AND c.review_status = 2 ' +
                            ' AND c.isdeleted = 0 ' +
                            ' ORDER BY m.publishdate DESC ' +
                            ' LIMIT 0,10';
                        connection.query(sql, [cinemaCode], function (queryErr, rows) {
                            if (queryErr) {
                                callback('获取片库失败！');
                                console.log('11');
                            } else {
                                if (rows.length == 0){
                                    arg1.movies = null;
                                    console.log('12');
                                } else {
                                    console.log('13');
                                    arg1.movies = rows;
                                    // arg1.movies = [];
                                    // for (let i=0;i<rows.length;i++){
                                    //     let tmp = {
                                    //         mid:rows[i].mid,
                                    //         poster:rows[i].poster,
                                    //         movie_name:rows[i].movie_name,
                                    //         score:rows[i].score,
                                    //     };
                                    //     arg1.movies.push(tmp);
                                    // }
                                }
                                callback(null,arg1);
                            }
                        });
                    }
                },
                function (arg1,callback) {
                    if (!openId){
                        arg1.collection_id = null;
                        console.log('8');
                        callback(null,arg1);
                    } else {
                        let sql = 'SELECT c.collection_id FROM member_cinema_collection as c ' +
                            ' INNER JOIN member_user as u ON u.member_id = c.member_id ' +
                            ' WHERE u.user_status = 0 ' +
                            ' AND u.openid = ? ' +
                            ' AND c.cinema_code = ? ';

                        connection.query(sql, [openId,cinemaCode], function (queryErr, rows) {
                            if (queryErr) {
                                console.log('9');
                                callback('获取收藏信息失败！');
                            } else {
                                console.log('10');
                                if (rows.length == 0) {
                                    arg1.collection_id = null;
                                } else {
                                    arg1.collection_id = rows[0].collection_id;
                                }
                                callback(null,arg1)
                            }
                        });
                    }
                },
                function (arg1,callback) {
                    let sql = " SELECT DISTINCT a.* " +
                        " FROM app_cinema_roomtype AS a " +
                        " INNER JOIN app_cinema AS b ON a.cinema_id = b.cinema_id" +
                        " INNER JOIN app_cinema_room AS c ON a.roomtype_id = c.roomtype_id" +
                        " WHERE a.roomtype_status = 1 AND a.online_booking_flag = 1" +
                        " AND c.room_status = 1 AND c.online_booking_flag = 1" +
                        " AND b.cinema_status = 1 AND b.online_booking_flag = 1 " +
                        " AND b.cinema_code = ?" +
                        " ORDER BY a.roomtype_price ASC ";
                    connection.query(sql, [cinemaCode], function (queryErr, rows) {
                        if (queryErr) {
                            console.log('11');
                            callback('获取可预约包房失败！');
                        } else {
                            console.log('12');
                            if (rows.length == 0){
                                arg1.roomtypes = null;
                            } else {
                                arg1.roomtypes = rows;
                            }
                            callback(null,arg1)
                        }
                    });
                }
            ],function (err,arg1) {
                if (err) {
                    res.render('wx-404', {
                        title: title,
                        errorMessage: '数据库链接错误'
                    });
                    connection.release();
                    return;
                }
                // console.log(arg1);
                arg1.title = title;
                res.render('wx-cinemahome', arg1);
                connection.release();
            })
        })

    }
};
